<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Department extends Model
{
    //

	use Notifiable;
	protected $table='department';
	protected $fillable=[
		'code','name','leader_id','parent_id','company_id','is_store'
	];
	protected $hidden=[];


	public function alls(){
		return $this->all();
	}
	public function department_station_desc($id){
		return $this->where(['id'=>$id])->first();
	}

//	获取所有的部门
	public function allDepartment($where){
		$data=$this->where(function ($query) use($where){
			$query->where(['company_id'=>$where['company_id']]);
			if($where['name'] !=null ){
				$query->where('name','like','%'.$where['name'].'%');
			}
			if($where['start_time'] !=null ){
				$query->where(['created_at','>',$where['start_time']]);
			}
			if($where['end_time'] !=null ){
				$query->where(['created_at','<',$where['end_time']]);
			}
			if($where['status'] !=null ){
				$query->where(['status'=>$where['status']]);
			}
			if($where['status_'] !=null){
				$query->where(['is_store'=>$where['status_']]);
			}
		})->with('userinfo')->with('parentinfo')->with('configuration_status')->get();
//			->paginate(config('app.config'));
		foreach($data as $kk=>$vv){
			$data[$kk]['create_at']=date('Y-m-d',strtotime($vv['created_at']));
		}
		return $data;
	}
//	获取所有的部门(加分页)
	public function allDepartments($where){
		$data=$this->where(function ($query) use($where){
			$query->where(['company_id'=>$where['company_id']]);
			if($where['name'] !=null ){
				$query->where('name','like','%'.$where['name'].'%');
			}
			if($where['start_time'] !=null ){
				$query->where(['created_at','>',$where['start_time']]);
			}
			if($where['end_time'] !=null ){
				$query->where(['created_at','<',$where['end_time']]);
			}
			if($where['status'] !=null ){
				$query->where(['status'=>$where['status']]);
			}
			if($where['status_'] !=null){
				$query->where(['is_store'=>$where['status_']]);
			}
		})->with('userinfo')->with('parentinfo')->with('configuration_status')->paginate(config('app.config'));
//			->paginate(config('app.config'));
		foreach($data as $kk=>$vv){
			$data[$kk]['create_at']=date('Y-m-d',strtotime($vv['created_at']));
		}
		return $data;
	}



//	获取所有部们(包括非店铺)
	public function departments($company_id){
		$data=$this->where(['company_id'=>$company_id,'status'=>1])->select('id','name')->get()->toArray();
		$newArr=['id'=>0,'name'=>'公司'];
		array_push($data,$newArr);
		return $data;
	}



//	获取没有店铺的部门
	public function no_store_depart($company_id){
		return $this->where(['company_id'=>$company_id,'is_store'=>1])->with('store')->with('userinfo')->with('parentinfo')->with('configuration_status')->get();
	}
	public function userinfo(){
		return $this->belongsTo('App\Model\Users','leader_id');
	}
	public function parentinfo(){
		return $this->belongsTo('App\Model\Department','parent_id');
	}


//	获取所在部门的所有成员
	public function department_member($department_name){
		return $this->where(function ($query) use($department_name){
			$query->where('name','like','%'.$department_name.'%');
		})->with('userinfo')->get();
	}


	public function configuration_status(){
		return $this->belongsTo('App\Model\Configuration_status','status','configuration_id');
	}


	public function store(){
		return $this->hasOne('App\Model\Store','department_id');
	}

}
